package com.ludaxu.google;

import com.ludaxu.google.fragment.AppFragment;
import com.ludaxu.google.fragment.BaseFragment;
import com.ludaxu.google.fragment.CategoryFragment;
import com.ludaxu.google.fragment.FragmentFactory;
import com.ludaxu.google.fragment.GameFragment;
import com.ludaxu.google.fragment.HomeFragment;
import com.ludaxu.google.fragment.SubjectFragment;
import com.ludaxu.google.fragment.TopFragment;
import com.ludaxu.google.holder.MenuHolder;
import com.ludaxu.google.tools.UiUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SearchView.OnQueryTextListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

public class MainActivity extends BaseActivity implements OnQueryTextListener {
	private DrawerLayout mDrawerLayout;
	@SuppressWarnings("deprecation")
	private ActionBarDrawerToggle drawerToggle;
	private ViewPager mViewPager;
	private PagerTabStrip pager_tab_strip;
	private String[] tab_names;// 标签的名字
	private FrameLayout fl_menu;// 菜单跟布局

	@Override
	protected void initActionBar() {
		ActionBar actionBar = getSupportActionBar();
		actionBar.setHomeButtonEnabled(true);
		actionBar.setDisplayHomeAsUpEnabled(true);

		drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer_am, R.string.open_drawer,
				R.string.open_drawer) {

			@Override
			public void onDrawerClosed(View drawerView) {
				Toast.makeText(MainActivity.this, "onDrawerClosed", 0).show();
				super.onDrawerClosed(drawerView);
			}

			@Override
			public void onDrawerOpened(View drawerView) {
				Toast.makeText(MainActivity.this, "onDrawerOpened", 0).show();
				super.onDrawerOpened(drawerView);
			}

		};
		mDrawerLayout.setDrawerListener(drawerToggle);
		// 将开关和actionbar建立关系
		drawerToggle.syncState();
	}

	@Override
	protected void init() {
		tab_names = UiUtils.getStringArray(R.array.tab_names);

	}

	@SuppressWarnings("deprecation")
	@Override
	protected void initView() {
		setContentView(R.layout.activity_main);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.dl);
		mViewPager = (ViewPager) findViewById(R.id.vp);
		pager_tab_strip = (PagerTabStrip) findViewById(R.id.pager_tab_strip);
		// 设置标签下划线的颜色
		pager_tab_strip.setTabIndicatorColor(getResources().getColor(
				R.color.indicatorcolor));

		mViewPager.setAdapter(new MainAdapter(getSupportFragmentManager()));
		mViewPager
				.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

					@Override
					public void onPageSelected(int position) {
						super.onPageSelected(position);
						BaseFragment fragment = FragmentFactory
								.createFragment(position);
						fragment.show();// 当切换界面的时候,重新请求服务器
					}

				});

		// sendBroadcast(new Intent("com.ludaxu.google.killall"));
		fl_menu = (FrameLayout) findViewById(R.id.fl_menu);
		MenuHolder holder = new MenuHolder();
		// holder.setInfo(data);
		fl_menu.addView(holder.getContentView());
	}

	private class MainAdapter extends FragmentStatePagerAdapter {

		public MainAdapter(FragmentManager fm) {
			super(fm);
		}

		/**
		 * 每个条目返回的fragment
		 */
		@Override
		public Fragment getItem(int position) {
			// 通过Fragment工厂 生产Fragment
			return FragmentFactory.createFragment(position);
		}

		/**
		 * 一共有几条条目
		 */
		@Override
		public int getCount() {
			return tab_names.length;
		}

		// 返回每个条目的标题
		@Override
		public CharSequence getPageTitle(int position) {
			// TODO Auto-generated method stub
			return tab_names[position];
		}

	}

	/**
	 * 创建菜单
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		// 如果当前运行程序的android版本大于11时,运行下面的代码
		if (android.os.Build.VERSION.SDK_INT > 11) {
			SearchView searchView = (SearchView) menu.findItem(
					R.id.action_search).getActionView();
			searchView.setOnQueryTextListener(this);
		}

		return true;
	}

	/**
	 * 处理菜单条目的点击事件
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_search) {
			return true;
		}
		return drawerToggle.onOptionsItemSelected(item)
				| super.onOptionsItemSelected(item);
	}

	/**
	 * 查询的文本发生变化
	 */
	@Override
	public boolean onQueryTextChange(String arg0) {
		Toast.makeText(MainActivity.this, arg0, 0).show();
		return false;
	}

	/**
	 * 当搜索提交的时候
	 */
	@Override
	public boolean onQueryTextSubmit(String arg0) {
		Toast.makeText(MainActivity.this, arg0, 0).show();
		return false;
	}

	public void click(View view) {
		Intent intent = new Intent(this, DetailActivity.class);
		startActivity(intent);
	}
}

/*
 * tab 标签备份
 * 
 * actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
 * 
 * Tab tab1 = actionBar.newTab().setText("标签1") .setTabListener(new
 * MyTabListener()); actionBar.addTab(tab1); Tab tab2 =
 * actionBar.newTab().setText("标签2") .setTabListener(new MyTabListener());
 * actionBar.addTab(tab2); Tab tab3 = actionBar.newTab().setText("标签3")
 * .setTabListener(new MyTabListener()); actionBar.addTab(tab3); Tab tab4 =
 * actionBar.newTab().setText("标签4") .setTabListener(new MyTabListener());
 * actionBar.addTab(tab4);
 * 
 * private class MyTabListener implements TabListener {
 * 
 * @Override public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
 * // TODO Auto-generated method stub
 * 
 * }
 * 
 * // 当tab标签被选中的时候,viewpager跳到指定的位置
 * 
 * @Override public void onTabSelected(Tab tab, FragmentTransaction arg1) {
 * mViewPager.setCurrentItem(tab.getPosition());
 * 
 * }
 * 
 * @Override public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
 * // TODO Auto-generated method stub
 * 
 * }
 * 
 * }
 * 
 * 
 * 
 * 
 * mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener()
 * { // 当viewpager页面发生变化的时候调用
 * 
 * @Override public void onPageSelected(int position) { // TODO Auto-generated
 * method stub super.onPageSelected(position);
 * getSupportActionBar().setSelectedNavigationItem( position); }
 * 
 * });
 */

